import React,{Component} from "react"

import {Link} from "react-router-dom";
import BScroll  from "better-scroll";

import img1 from "../../images/num1.png"
import img2 from "../../images/num2.png"
import img3 from "../../images/num3.png"
import editImg from "../../images/mywork.png"
import addrankImg from "../../images/addrank.png"
import "./index.css"

import {getCourseList,getRankList,jixiucloudlogin,getUserInfoadmin} from "../../api/course"

function toZero(n){
  return n>9 ? ""+n:"0"+n
}



class Index extends Component{
    constructor(props){
    	super(props);
    	this.state = {
            username:'',
            password:'',
            courselink:true
    	}
    }
    
    componentDidMount(){
  
    }
   

    changeinput(name,e){
        if(name == 'username'){
          this.setState({
            username: e.target.value
          })
        }else if(name == 'password'){
            this.setState({
                password:e.target.value
              })
           
        }

    }

   login(){
       if(!this.state.username){
           alert('账号不能为空！')
           return
       }else if(!this.state.password){
        alert('密码不能为空！')
        return
       }
       let data = {
        "grant_type": "jixiuapp",
        "client_id": "game",
        "client_secret": "swagger",
        "username": '86'+this.state.username,
        "password":  this.state.password,
        "scope": "openid profile email phone role permission isms.all"
       }
    jixiucloudlogin(data).then(res=>{
         if(res.status == 200){
            let token = res.data&&res.data.jx_access_token?res.data.jx_access_token:''
            getUserInfoadmin(token).then(data=>{
                if(data.data.Role == '1'){
                    localStorage.setItem('loginadmin',true)
                    this.props.history.push('/fanscourse/courselist')
                }else{
                    alert('非管理员不能登录')
                }
              
            })
           
         }
    },error=>{
        alert('输入有误！')
    })
   }

	render(){
		let {tabbar,nowIndex,ranklist,hotlist,nowDate} = this.state;
		return(
            <div className="loginadmin">
                <div className='contain'>
                <form id="regForm" >
                     <div className='loginadmincon'>
                    <div className='label'>账号：</div>
                    <div className='input'><input type='text' name="username" onChange={this.changeinput.bind(this,'username')}></input></div>
                        </div>
                        <div className='loginadmincon'>
                            <div className='label'>密码：</div>
                            <div className='input'><input type='password' name="password" onChange={this.changeinput.bind(this,'password')}></input></div>
                        </div>
                    <div onClick={this.login.bind(this)} className='loginbtn'>登录</div>
                </form>
               
             
               </div>
            </div>
		)
	}
}


export default Index